using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using DAL;
using BUS;
using Utility;

public partial class admin_Danhsachsanpham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
        }
    }
    private void LoadDropDownList()
    {

        ddl_loai .DataSource = tbLoaiMHBUS.GetAll();
        ddl_loai.DataTextField = tbLoaiMHColumns.Ten.ToString();
        ddl_loai.DataValueField = tbLoaiMHColumns.MaLoaiMH.ToString();
        ddl_loai.DataBind();
        ddl_loai.Items.Insert(0, new ListItem("All", "0"));
        ddl_ncc.DataSource = tbDoanhNghiepBUS.GetAll();
        ddl_ncc.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_ncc.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_ncc.DataBind();
        ddl_ncc.Items.Insert(0, new ListItem("All", "0"));
        dd_tt.DataSource = tbTrangThaiBUS.GetAll();
        dd_tt.DataTextField = tbTrangThaiColumns.Ten.ToString();
        dd_tt.DataValueField = tbTrangThaiColumns.MaTT.ToString();
        dd_tt.DataBind();
        dd_tt.Items.Insert(0, new ListItem("All", "0"));

    }
    private void LoadData()
    {
        Criteria cri = new Criteria();

        int total = 0;
        if (txt_Tensp.Text != "")
            cri.add(Condition.AND, tbSanPhamColumns.TenSP.ToString(), Condition.LIKE, txt_Tensp.Text);
        if (ddl_loai.SelectedValue != "0")
            cri.add(Condition.AND, tbSanPhamColumns.MaSP.ToString(), Condition.EQUAL, int.Parse(ddl_loai.SelectedValue));
        if (ddl_ncc.SelectedValue != "0")
            cri.add(Condition.AND, tbSanPhamColumns.MaDN.ToString(), Condition.EQUAL, int.Parse(ddl_ncc.SelectedValue));
        if (dd_tt.SelectedValue != "0")
            cri.add(Condition.AND, tbTrangThaiColumns.MaTT.ToString(), Condition.EQUAL, int.Parse(dd_tt.SelectedValue));



        tbSanPhamCollection sanphamCollection = tbSanPhamBUS.Get(cri.Criter, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize,
        tbSanPhamColumns.MaSP.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = sanphamCollection;
        Repeater1.DataBind();
        hyperlinkPager.ItemCount = total;
        //if (total <= hyperlinkPager.PageSize)
        //hyperlinkPager.Visible = false;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
        || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hyp_Tensp = (HyperLink)e.Item.FindControl("hyp_Tensp");
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            Literal lit_Ma = (Literal)e.Item.FindControl("lit_Ma");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            Literal lit_Mamh = (Literal)e.Item.FindControl("lit_Mamh");
            Literal lit_Madn = (Literal)e.Item.FindControl("lit_Madn");
            Literal lit_Madd = (Literal)e.Item.FindControl("lit_Madd");
            Literal lit_Matt = (Literal)e.Item.FindControl("lit_Matt");

            Image img_hinhanh = (Image)e.Item.FindControl("img_hinhanh");
            tbSanPham sanpham = (tbSanPham)e.Item.DataItem;
            tbLoaiMH loai = tbLoaiMHBUS.GetByID(sanpham.MaLoai);
            hyp_Tensp.Text = sanpham.TenSP;
            if (loai != null)
                lit_Mamh.Text = loai.Ten;
            tbDoanhNghiep doanhnghiep = tbDoanhNghiepBUS.GetByID(sanpham.MaDN);
            if (doanhnghiep != null)
                lit_Madn.Text = doanhnghiep.TenDN;
            tbDiemDen diemden = tbDiemDenBUS.GetByID(sanpham.DiemDen);
            if (diemden != null)
                lit_Madd.Text = diemden.TenDiemDen;
            tbTrangThai trangthai = tbTrangThaiBUS.GetByID(sanpham.TrangThai);
            if (trangthai != null)
                lit_Matt.Text = trangthai.Ten;
            hyp_Tensp.NavigateUrl = "Chitietsanpham.aspx?id="+sanpham.MaSP.ToString()  ;
            img_hinhanh.ImageUrl = "~/Photos/Thumb/" + sanpham.Hinhminhhoa;
            //lit_Ma.Text = sanpham.Masp .ToString();
            //hyp.NavigateUrl = "Danhsachsp.aspx?id=" + sanpham.Masp.ToString();
            hpl_Edit.NavigateUrl = "Suasanpham.aspx?id=" + sanpham.MaSP.ToString();
            hf_id.Value = sanpham.MaSP.ToString();
            //hyp.Text = sanpham.Tensp.ToString();
        }
    }
    protected void lbt_xoa_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                CheckBox cb_xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_xoa");
                if (cb_xoa.Checked == true)
                {
                    HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                    int id = int.Parse(hf_id.Value);
                    tbSanPham sp = tbSanPhamBUS.GetByID(id);
                    //if (sp.Hinh != "no_images.jpg")
                    //{
                    //    System.IO.File.Delete(Server.MapPath("~/Photos/" + sp.Hinh));
                    //    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + sp.Hinh));
                    //}
                    tbSanPhamBUS.Delete(id);
                }
            }
            Response.Redirect(Request.RawUrl);
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void hyperlinkPager_Command(object sender, CommandEventArgs e)
    {
        hyperlinkPager.CurrentIndex = Convert.ToInt32(e.CommandArgument);
        LoadData();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}
